//#########################################################################
//                              WackiEngie
//           Copyright (c) 2014 Marc Wacker. All rights reserved.
//#########################################################################

#include "WeTexture.h"

namespace WackiEngine
{


	void Texture::generateMipmap()
	{
		assert(_handle != GL_ZERO);
		assert((_target == GL_TEXTURE_1D) ||
			(_target == GL_TEXTURE_2D) ||
			(_target == GL_TEXTURE_3D) ||
			(_target == GL_TEXTURE_1D_ARRAY) ||
			(_target == GL_TEXTURE_2D_ARRAY) ||
			(_target == GL_TEXTURE_CUBE_MAP));
		glGenerateMipmap(_target);
	}

	void Texture::init(GLenum target)
	{
		_target = target;
		_size[0] = _size[1] = _size[2] = (GLsizei)1;
		_texelSize[0] = _texelSize[1] = _texelSize[2] = (GLfloat)1.0f;

		glGenTextures(1, &_handle);

		bind();
	}

	void Texture::updateParameters()
	{
		GLenum tg = _target;
		if(tg == GL_TEXTURE_CUBE_MAP)
			tg = GL_TEXTURE_CUBE_MAP_POSITIVE_X;

		glGetTexLevelParameteriv(tg, 0, GL_TEXTURE_INTERNAL_FORMAT, &_format);
		glGetTexLevelParameteriv(tg, 0, GL_TEXTURE_WIDTH, &_size[0]);
		glGetTexLevelParameteriv(tg, 0, GL_TEXTURE_HEIGHT, &_size[1]);
		if(_target == GL_TEXTURE_2D_MULTISAMPLE)
			glGetTexLevelParameteriv(tg, 0, GL_TEXTURE_SAMPLES, &_size[2]);
		else
			glGetTexLevelParameteriv(tg, 0, GL_TEXTURE_DEPTH, &_size[2]);

		_texelSize[0] = 1.0f / (GLfloat)_size[0];
		_texelSize[1] = 1.0f / (GLfloat)_size[1];
		_texelSize[2] = 1.0f / (GLfloat)_size[2];
	}

}